package com.yuxi.jedis;

import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.Map;
import java.util.Set;

/**
 * jedis 使用
 * Created by yuxi on 16/12/30.
 */
@RestController
public class JedisController {

    @Autowired
    Jedis jedis;


    @ApiOperation("jedis test")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "内部服务错误")
    })
    @RequestMapping(value = "/jedis", method = RequestMethod.GET)
    public Object jedisTest() {
        //string
        System.out.println(jedis.get("yuxi"));
        System.out.println(jedis.set("yuxi", "yuxi"));
        System.out.println(jedis.set("yuxi", "yuxi", "NX", "EX", 1));
        //hash
        jedis.hset("number", "one", "1");
        jedis.hset("number", "two", "2");
        jedis.hset("number", "three", "3");
        Map<String, String> hashMap = jedis.hgetAll("number");
        for (Map.Entry<String, String> stringStringEntry : hashMap.entrySet()) {
            System.out.println(stringStringEntry.getValue());
        }
        String obj = jedis.hget("number", "one");
        System.out.println(obj);

        // list
        jedis.lpush("list", "1");
        jedis.lpush("list", "2");
        jedis.lpush("list", "3");
        jedis.lpop("list");

        //zset
        jedis.zadd("zset", 1, "1");
        jedis.zadd("zset", 2, "2");
        jedis.zadd("zset", 3, "3");
        Set<String> set = jedis.zrange("zset", 0, -1);
        for (String s : set) {
            System.out.println(s);
        }
        long l = jedis.zrem("zset", "1");
        System.out.println(l);

        //set
        jedis.sadd("set", "1");
        jedis.sadd("set", "2");
        jedis.sadd("set", "3");
        jedis.sadd("set", "4");
        Set set1 = jedis.smembers("set");
        for (Object o : set1) {
            System.out.println(o);
        }
        String val = jedis.spop("set");
        System.out.println(val);

        return jedis.get("yuxi");
    }

}
